package com.like.fragment;


import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.like.adapter.ArticleReplyAdapter;
import com.like.entity.EventWrapper;
import com.like.entity.GeneralResponse;
import com.like.entity.InfoListResp;
import com.like.entity.InfoGetReplyReq;
import com.like.entity.InfoGetReplyResp;
import com.like.hospitalworld.InfoDetailActivity;
import com.like.hospitalworld.R;
import com.like.likeutils.network.RetrofitCallback;
import com.like.network.InfoService;
import com.like.network.RetrofitUtil;
import com.like.utils.Constants;

import org.greenrobot.eventbus.EventBus;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Response;

@ContentView(R.layout.fragment_info_detail_reply)
public class InfoDetailReplyFragment extends BaseFragment {

    @ViewInject(R.id.reply_list)
    PullToRefreshListView mReplyList;

    private ArticleReplyAdapter mReplyAdapter;
    private InfoListResp.InformationBase mInfo;
    private InfoService mInfoService;
    private int mCurrentPage = 0;
    private int mPageNum = 15;

    @Override
    protected void onCreateView() {
        super.onCreateView();
        mInfoService = RetrofitUtil.getService(InfoService.class);
        mReplyList.setMode(PullToRefreshBase.Mode.BOTH);
        mReplyList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                mCurrentPage = 0;
                getReply(mCurrentPage, false);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                mCurrentPage += 1;
                getReply(mCurrentPage, true);
            }
        });
        mReplyList.getRefreshableView().setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        EventBus.getDefault().post(new EventWrapper(null, InfoDetailActivity.class, InfoDetailActivity.REQUEST_CLOSE_COMMENT));
                        break;
                }
                return false;
            }
        });
        getReply(mCurrentPage, false);
    }

    public void setInfoBase(InfoListResp.InformationBase infoBase) {
        mInfo = infoBase;
    }

    public void getReply(int currentPage, final boolean append) {
        mCurrentPage = currentPage;
        if (m == null)
            return;
        InfoGetReplyReq req = new InfoGetReplyReq(mInfo.id, currentPage, mPageNum);
        Call<GeneralResponse<InfoGetReplyResp>> call = mInfoService.getInfoReplyList(Constants.getGUID(), m.getToken(), req);
        call.enqueue(new RetrofitCallback<GeneralResponse<InfoGetReplyResp>>() {
            @Override
            public void onResponse(Call<GeneralResponse<InfoGetReplyResp>> call, Response<GeneralResponse<InfoGetReplyResp>> response) {
                mReplyList.onRefreshComplete();
                if (!response.isSuccessful())
                    return;
                InfoGetReplyResp infoReplyResp = response.body().data;
                List<InfoGetReplyResp.InformationComment> comments = infoReplyResp.comments;
                if (mReplyAdapter == null) {
                    mReplyAdapter = new ArticleReplyAdapter(comments, m);
                    mReplyList.setAdapter(mReplyAdapter);
                } else
                    mReplyAdapter.update(comments, append);
            }
        });
    }

    @Event(value = R.id.reply_list, type = AdapterView.OnItemClickListener.class)
    private void replyListClick(AdapterView<?> parent, View view, int position, long id) {
        position -= 1;
        InfoGetReplyResp.InformationComment comment = mReplyAdapter.getItem(position);
        EventBus.getDefault().post(new EventWrapper(comment, InfoDetailActivity.class, InfoDetailActivity.REQUEST_INFO_REPLY));
    }

}
